package com.an.practise;

import java.util.Random;
import java.util.Scanner;

/**
 * @author Li
 * @Date 2023/4/10 11:32
 */
public class SumArrayTest {
    public static void main(String[] args) {
        /**
         * 随机生成一个数，并求和
         */
        int[] array = getArray(10);
        printArray(array);
        int sum = SumArray(array);
        System.out.println("数组和为:"+sum);

    }



    /**
     * 随机输入10以内的整数，创建一个随机数组
     * @param size
     * @return
     */
    private static int[] getArray(int size) {
       int[] array = new  int[size];  //new一个新的数组，大小为size
        Random random = new Random();  //创建随机数
        for (int i = 0; i < array.length; i++) {
            array[i] = random.nextInt(10); //随机数赋值给array数组
        }
        return array;
    }

    /**
     * 打印出数组
     * @param array
     */
    private static void printArray(int[] array) {
        System.out.print("[");
        for (int i = 0; i < array.length; i++) {
            if (i==array.length-1){
                System.out.println(array[i]+"]");
            }else {
                System.out.print(array[i]+",");
            }
        }
    }

    /**
     *数组求和
     * @return
     */

     private static int SumArray(int[] array) {
         int sum = 0;
         for (int i = 0; i <array.length ; i++) {
             sum+=array[i];
         }
         return sum;

}

}
